package personal.wang.juc.completableFuture.React;


import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

/**
 * @className: Dispatcher
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/7 15:31
 */
public class Dispatcher {
    private Map<String,ResultFuture> map = new ConcurrentHashMap<>();
    public static void main(String[] args) {
        new Dispatcher().run();
    }
    public void run(){
        for (int i = 0; i < 100 ; i++) {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ResultFuture resutlFuture = new ResultFuture(UUID.randomUUID().toString() , this);

            int r = new Random().nextInt(10);
            map.put(resutlFuture.getUuid() , resutlFuture);
            CompletableFuture.supplyAsync(()->{
                try {
                    Thread.sleep(r*1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return r ;
            }).thenAcceptAsync(x -> {
                resutlFuture.complete(x+"");
            } , Executors.newFixedThreadPool(2));
        }

    }
}
